//
//  StoreDetailViewController.m
//  much
//
//  Created by weijunjie on 2019/1/8.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "StoreDetailViewController.h"
#import "NewReadIndexCell.h"
#import "GoodsDetailViewController.h"
#import "StoreDetailBottomToolView.h"
#import "StoreDetailTopToolView.h"
#import "GoodsModel.h"
#import "GoodsDetailWebViewController.h"
#import "AffirmOrderViewController.h"
#import "OrderDataModel.h"
@interface StoreDetailViewController()
<
UICollectionViewDataSource,
UICollectionViewDelegate
>
@property (strong, nonatomic) UICollectionView * collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout * flowLayout;
@property (strong, nonatomic) NSMutableArray * dataSource;
@property (strong, nonatomic) StoreDetailBottomToolView * bottomToolView;
@property (strong, nonatomic) StoreDetailTopToolView * topToolView;
@property (nonatomic, strong) GoodsModel *model; // 滚动视图的底视图
@property (nonatomic, strong) GoodsDetailViewController *detailViewController; // 商品详情
@property (nonatomic, strong) GoodsDetailWebViewController *detailWebViewController; // 网页详情
@end
@implementation StoreDetailViewController

- (void)_reLayout {
    [self setupNavigationItems];
    [self addChildViewController:self.detailViewController];
    [self addChildViewController:self.detailWebViewController];
    [self.collectionView reloadData];
    self.detailWebViewController.spuSid = self.spuSid;
    
    // 请求商品详情数据
    [self requesetStoreDetailBasicInfo];
}

- (void)setupNavigationItems {
    __weakConfigure
    [self.navigationView addCustomView:self.topToolView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(__weakSelf.navigationView).offset(-40);
        make.left.equalTo(__weakSelf.navigationView).offset(40);
        make.bottom.equalTo(__weakSelf.navigationView);
        make.height.mas_equalTo(44);
    } callback:^(UIView *view) {
        
    }];
}

- (StoreDetailTopToolView *)topToolView {
    if (!_topToolView) {
        _topToolView = [[StoreDetailTopToolView alloc] init];
        __weakConfigure
        _topToolView.switchActionBlock = ^(StoreDetailSwitchType type) {
            CGFloat offsetX = ScreenWidth * type;
            [__weakSelf.collectionView setContentOffset:CGPointMake(offsetX, 0)];
            [__weakSelf.topToolView moveLineWithOffsetX:offsetX];
        };
    }
    return _topToolView;
}

- (GoodsDetailViewController *)detailViewController {
    if (!_detailViewController) {
        _detailViewController = [[GoodsDetailViewController alloc] init];
        __weakConfigure
        _detailViewController.canSlid = ^(BOOL slid) {
            __weakSelf.topToolView.slidToBottom = !slid;
            __weakSelf.collectionView.scrollEnabled = slid;
        };
    }
    return _detailViewController;
}

- (GoodsDetailWebViewController *)detailWebViewController {
    if (!_detailWebViewController) {
        _detailWebViewController = [[GoodsDetailWebViewController alloc] init];
    }
    return _detailWebViewController;
}

- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - NavigationHeight_N() - 49);
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.minimumLineSpacing = 0;
    }
    return _flowLayout;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavigationHeight_N(), ScreenWidth, ScreenHeight - NavigationHeight_N() - 49) collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
        
        // 内容cell
        [_collectionView registerClass:[NewReadIndexCell class] forCellWithReuseIdentifier:[NewReadIndexCell cellClass]];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.navigationView.mas_bottom);
            make.bottom.equalTo(self.bottomToolView.mas_top);
        }];
    }
    return _collectionView;
}

- (StoreDetailBottomToolView *)bottomToolView {
    if (!_bottomToolView) {
        _bottomToolView = [[StoreDetailBottomToolView alloc] init];
        [self.view addSubview:_bottomToolView];
        [_bottomToolView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(49);
        }];
        __weakConfigure
        _bottomToolView.bottomViewBlock = ^(NewStoreDetailSpecificationsButtonType type) {
            [__weakSelf bottomToolViewActionWithType:type];
        };
    }
    return _bottomToolView;
}

#pragma mark - <UICollectionViewDataSource, UICollectionViewDelegate>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewReadIndexCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NewReadIndexCell cellClass] forIndexPath:indexPath];
    
    [cell addSubview:self.childViewControllers[indexPath.row].view];
    return cell;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (![scrollView isKindOfClass:[UICollectionView class]]) return;
    [self.topToolView moveLineWithOffsetX:scrollView.contentOffset.x];
}

#pragma mark - event
- (void)bottomToolViewActionWithType:(NewStoreDetailSpecificationsButtonType)type {
    switch (type) {
        case CallPhoneToService: // 消息
            break;
        case GotoShoppingCarButtonType: // 进入购物车
            break;
        case InsertShoppingCartButtonType: // 加入购物车
            [self insertShoppingCart];
            break;
        case NowBuyButtonType: // 立即购买
            [self preOrderToBuy];
            break;
        case ClickGoodsCollectionButtonType: // 收藏
            break;
            
        default:
            break;
    }
}

// 加入购物车
- (void)insertShoppingCart {
    
}

// 预下单
- (void)preOrderToBuy {
    // 总价
    CGFloat totalPrice = self.model.number * [self.model.sellingPrice floatValue];
    totalPrice = 292;
    NSString *totalPriceString = [NSString stringWithFormat:@"%.2f", totalPrice];
    GoodsModel *model = [[GoodsModel alloc] init];
    model.amount = @"252";
    model.number = 2;
    model.sid = @"";
    model.skuSid = @"oACZg(";
    model.spuSid = @"oVCZZa";
    model.type = @"MATERIAL_OBJECT";
    model.version = @"10003402";
    [MuchStoreRequestManager storeGoodsPreOrderWithAmount:totalPriceString goodsArray:@[model] successBlock:^(id  _Nonnull responseObject) {
        AffirmOrderViewController *orderVC = [[AffirmOrderViewController alloc] init];
        orderVC.orderDataModel = responseObject;
        [self.navigationController pushViewController:orderVC animated:YES];
    } failureBlock:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

#pragma mark - request
/**
 商品详情基础数据
*/
- (void)requesetStoreDetailBasicInfo {
    [MuchStoreRequestManager storeDetailInfoWithSpuSid:self.spuSid successBlock:^(GoodsModel *  _Nonnull model) {
        self.detailViewController.model = model;
    } failureBlock:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

@end
